<%--
  Created by IntelliJ IDEA.
  User: dj
  Date: 2018-08-24
  Time: 17:16
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <%@ include file="/common/baseMeta.jsp" %>

    <script type="text/javascript" src="/static/js/common.js"></script>
    <title>环境检测</title>
    <script type="text/javascript">

        var uid = "${sysStaffEntity.uid}";
        var lname = "${sysStaffEntity.lname}"; //登录名
        var uname = "${sysStaffEntity.uname}"; //用户名
        var role_name = ""; //用户角色

        //新紫外线灯管
        var jcjg_zwxdg = [{"code": "大于70uw/cm2", "name": "大于70uw/cm2"},
            {"code": "大于90uw/cm2", "name": "大于90uw/cm2"},
            {"code": "小于70uw/cm2", "name": "小于70uw/cm2"}
        ];
        //生物监测
        var jcjg_swjc = [{"code": "对照阳性监测阳性", "name": "对照阳性监测阳性"},
            {"code": "对照阳性监测阴性", "name": "对照阳性监测阴性"},
            {"code": "对照阴性监测阴性", "name": "对照阴性监测阴性"}
        ];
        //其他
        var jcjg_qt = [{"code": "<1", "name": "<1"},
            {"code": "不得检出", "name": "不得检出"},
            {"code": "无菌培养", "name": "无菌培养"},
            {"code": "无菌生长", "name": "无菌生长"},
            {"code": "无致病菌", "name": "无致病菌"},
        ];
        $(function () {

            $.ajaxSetup({
                async: false
            });
            //获取角色信息
            $.getJSON("/commapiV2", {p_dm: "Sys_User_Roles_Get", userid: lname}, function (data) {
                if (data.success) {
                    role_name = data.Table[0]["role_name"];
                    if (role_name == "检验科组") {
                        $("#sjzt option[value='']").remove();
                        $("#sjzt option[value='未发送']").remove();
                        $("#sjzt option[value='已作废']").remove();
                        $("#tj").hide();
                        $("#xg").hide();
                        $("#fs").hide();
                        $("#zf").hide();
                    }
                    if (role_name == "ICU护士组" || role_name == "护士组" || role_name == "医生组" || role_name == "ICU医生组"|| role_name == "院感科组") {
                        $("#sjzt option[value='']").remove();
                        $("#bcjyjg").hide();
                        $("#sh").hide();
                    }
                }
            }, true);

            $.ajaxSetup({
                async: true
            });
            $.ajaxSetup({
                async: false
            });
            //初始化采样人所在科室
            $.getJSON("/commapiV2", {p_dm: "sys_ks_GetByUser", userid: lname}, function (data) {
                if (data.success) {
                    $('#ks').combobox({
                        data: data.Table,
                        valueField: 'code',
                        textField: 'name'
                    });
                    $(ks).combobox('select', data.Table[0].code);
                }
            }, true);
            $.ajaxSetup({
                async: true
            });

            $("#Maingrid").height(findDimensions_height() / 2 - 50);
            $("#Contentgrid").height(findDimensions_height() / 2 - 10);
            initMaingrid();
            initContentgrid();
            //主表信息
            BandMainGrid();
        })
        function BandMainGrid() {
            var postdata = {
                p_dm: "Hjjc_JCMain_Get",
                zt: $("#sjzt").val(),
                Sdate: $("#sdate").val() == "" ? '2018-01-01' : $("#sdate").val(),
                Edate: $("#edate").val() == "" ? '2089-03-01' : $("#edate").val(),
                userid: lname,
                ksname: $('#ks').combobox('getText')
            };
            //从API获取数据，并绑定
            $.getJSON("/commapiV2", postdata, function (data) {
                if (data.success) {
                    var griddata = {};
                    griddata.rows = data.Table;
                    griddata.total = data.totalcount;
                    $('#Maingrid').datagrid('loadData', griddata);
                    $("#Maingrid").datagrid("selectRow", 0);
                    var row = $('#Maingrid').datagrid('getSelected');
                    BandContentGrid(row.id);
                }
            });
        }
        //从表信息
        function BandContentGrid(id) {
            $.getJSON("/commapiV2", {
                p_dm: "Hjjc_content_get",
                mid: id
            }, function (data) {
                if (data.success) {
                    var griddata = {};
                    griddata.rows = data.Table;
                    griddata.total = data.totalcount;
                    $('#Contentgrid').datagrid('loadData', griddata);
                }
            }, true);
        }
        function initMaingrid() {

            $('#Maingrid').datagrid({
                rownumbers: true,//显示行号
                singleSelect: true,//只显示一行
                pagination: true,//启用分页
                fitColumns: true,
                sortable: true, //启用排序列
                collapsible: true,
                idField: 'id',
                pageSize: 50,
                pageList: [50, 100, 150],
                columns: [
                    [
                        {field: 'szks', title: '所在科室', width: '120px', align: 'center'},
                        {field: 'cydate', title: '采样时间', width: '120px', align: 'center'},
                        {field: 'cyr', title: '采样人', width: '120px', align: 'center'},
                        {field: 'jcjb', title: '检测级别', width: '90px', align: 'center'},
                        {field: 'jyr', title: '检验人', width: '80px', align: 'center'},
                        {field: 'shr', title: '审核人', width: '80px', align: 'center'},
                        {field: 'zt', title: '数据状态', width: '60px', align: 'center'},
                        {field: 'id', title: 'id', hidden: true}
                    ]
                ],
                onClickRow: function (index, field, value) {
                    var row = $('#Maingrid').datagrid('getSelected');
                    BandContentGrid(row.id);
                    if (row.jcjb == "自测自检") {
                        $("#bcjyjg").show();
                    }
                }
            }).datagrid('clientPaging');
        }
        function initContentgrid() {
            $('#Contentgrid').datagrid({
                rownumbers: true,//显示行号
                singleSelect: true,//只显示一行
                pagination: false,//启用分页
                fitColumns: true,
                sortable: true, //启用排序列	
                collapsible: true,
                idField: 'mid',
                columns: [
                    [
                        {field: 'jcks', title: '检测科室', width: '120px', align: 'center'},
                        {field: 'jclx', title: '检测类型', width: '120px', align: 'center'},
                        {field: 'jyxm', title: '检验项目', width: '90px', align: 'center'},
                        {field: 'bz', title: '备注', width: '120px', align: 'center'},
                        {field: 'jcbb', title: '检测标本', width: '100px', align: 'center'},
                        {field: 'jyjz', title: '检测菌种', width: '90px', align: 'center'},
                        {
                            field: 'jyjg', title: '检验结果', width: '150px', align: 'center',
                            editor: {
                                type: 'combobox',
                                options: {
                                    valueField: 'code',
                                    textField: 'name',
                                    method: 'get',
                                    required: true
                                }
                            }
                        },
                        {
                            field: 'zbj', title: '致病菌', width: '200px', align: 'center',
                            editor: {
                                type: 'combobox',
                                options: {
                                    valueField: 'code',
                                    textField: 'name',
                                    multiple: true
                                }
                            }
                        },
                        {field: 'md', title: '检测目的', width: '80px', align: 'center'},
                        {field: 'ishg', title: '是否合格', width: '80px', align: 'center'},
                        {field: 'id', title: 'id', hidden: true},
                        {field: 'mid', title: 'id', hidden: true}
                    ]
                ],
                onClickRow: function (index, row) {
                    $("#Contentgrid").datagrid('selectRow', index);
                    $("#Contentgrid").datagrid('beginEdit', index);
                    var smEditor = $('#Contentgrid').datagrid('getEditor', {
                        index: index,
                        field: 'jyjg'
                    });
                    var zbjEditor = $('#Contentgrid').datagrid('getEditor', {
                        index: index,
                        field: 'zbj'
                    });
                    //datatype处理统计方法
                    var data = {};
                    if (row.jyxm == "生物监测")
                        data = jcjg_swjc;
                    else if (row.jyxm == "生物监测结果" || row.jyxm == "使用中紫外线灯管")
                        data = jcjg_zwxdg;
                    else
                        data = jcjg_qt;
                    $(smEditor.target).combobox("loadData", data);
                    //致病菌
                    $.getJSON("/commapiV2", {
                        p_dm: "Sys_Dictionary_Item_GetItemPym",
                        dic_name: "病原体"
                    }, function (data) {
                        if (data.success) {
                            $(zbjEditor.target).combobox("loadData", data.Table);
                        }
                    }, true);
                }
            })
        }
        //修改检测信息
        function EditeJyinfo() {

            var row = $('#Maingrid').datagrid('getSelected');
            if (row.zt == '已发送') {
                alert("数据已发送不能编辑，如需修改请作废重新填写并发送！");
                return;
            }
            var url = "HjjcEdite?PageMode=Edite&ID=" + row.id;
            layer.open({
                type: 2,
                title: '添加数据',
                skin: 'layui-layer-demo', //样式类名
                shadeClose: true, //开启遮罩关闭
                shade: [0.3, '#000000'],
                maxmin: true, //开启最大化最小化按钮
                area: ['1000px', '570'],
                content: url,
                btn: ['确定', '取消'],
                yes: function (index, layero) {
                    var body = layer.getChildFrame('body', index);
                    var iframeWin = window[layero.find('iframe')[0]['name']];//得到iframe页的窗口对象，执行iframe页的方法：
                    iframeWin.SaveData();//调用子页面的方法，得到子页面返回的ids 
                    loadList();
                }
            });
        }
        //修改数据状态
        function UpdateJyinfo(ztstr) {
            var row = $('#Maingrid').datagrid('getSelected');
            //已经填写过检验结果的数据
            if (ztstr == '已发送') {
                if (row.zt == "已发送") {
                    alert('该数据已经发送过了，请务再次点击！');
                    return;
                }
            } 
            if (ztstr == '已审核') {
				if(row.jyr == "")
				{ 
                    alert('请先发送数据！');
                    return;
				}
                $.ajaxSetup({
                    async: false
                });
                $.getJSON("/commapiV2", {
                    p_dm: "Hjjc_JCMain_SaveSHR",
                    shr: uname,
                    id: row.id
                }, function (data) {
                    if (data.success) { 
                        BandMainGrid();
                    }
                }, true);

                $.ajaxSetup({
                    async: true
                });

            }
            $.getJSON("/commapiV2", {
                p_dm: "Hjjc_JCMain_UpdateZt",
                zt: ztstr,
                id: row.id
            }, function (data) {
                if (data.success) {

                    BandMainGrid();
                }
            }, true);

        }
        function AddJyinfo() {
            var url = "HjjcEdite";
            layer.open({
                type: 2,
                title: '添加数据',
                skin: 'layui-layer-demo', //样式类名
                shadeClose: true, //开启遮罩关闭
                shade: [0.3, '#000000'],
                maxmin: true, //开启最大化最小化按钮
                area: ['1000px', '570'],
                content: url,
                btn: ['确定', '取消'],
                yes: function (index, layero) {
                    var body = layer.getChildFrame('body', index);
                    var iframeWin = window[layero.find('iframe')[0]['name']];//得到iframe页的窗口对象，执行iframe页的方法：
                    iframeWin.SaveData();//调用子页面的方法，得到子页面返回的ids 
                    loadList();
                }
            });
        }
        function SaveJyjg() {
            var rows = $("#Contentgrid").datagrid("getRows");
            var rowMain = $('#Maingrid').datagrid('getSelected');
            $("textbox").each(function () {
                var value = $(this).val();
            });
            for (var i = 0; i < rows.length; i++) {
                //获取dataGrid中下拉框的值
                var JyjgEditor = $('#Contentgrid').datagrid('getEditor', {
                    index: i,
                    field: 'jyjg'
                });
                var zbjEditor = $('#Contentgrid').datagrid('getEditor', {
                    index: i,
                    field: 'zbj'
                });
                if (JyjgEditor != null && zbjEditor != null) {
                    var strjyjg = $(JyjgEditor.target).combobox('getValue');
                    var strzbj = $(zbjEditor.target).combobox('getText');
                    //检验是否合格
                    $.getJSON("/commapiV2", {
                            p_dm: "Hjjc_content_chackishg",
                            zbj: strzbj,
                            jyjg: strjyjg,
                            jyxm: rows[i].jyxm,
                            id: rows[i].id,
                            isnum: isNumber(strjyjg)
                        }, function (data) {
                            if (data.success) {
                                var row = $('#Maingrid').datagrid('getSelected');
                                BandContentGrid(row.id);
                            }
                        }
                    );

                }
            }
            $.getJSON("/commapiV2", {
                p_dm: "Hjjc_JCMain_SaveJYR",
                jyr: uname,
                id: rowMain.id
            }, function (data) {
                if (data.success) {
                    alert('保存成功！');
                    BandMainGrid();
                }
            });
        }

        function isNumber(val) {
            var regPos = /^\d+(\.\d+)?$/; //非负浮点数
            var regNeg = /^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$/; //负浮点数
            if (regPos.test(val) || regNeg.test(val)) {
                return true;
            } else {
                return false;
            }

        }

        function loadList() {
            BandMainGrid();
        }
    </script>
</head>
<body>
<div class="navheader form-inline" style="height:40px;margin-bottom: 0px;">
    <div class=" form-group">
        <label for="ZYNumber" style="padding:5px 10px 0 20px;">状态</label>
        <select id="sjzt">
            <option value=''></option>
            <option value='未发送' selected="selected">未发送</option>
            <option value='已发送'>已发送</option>
            <option value='已审核'>已审核</option>
            <option value='已作废'>已作废</option>
        </select>
        <label for="ZYNumber" style="padding:5px 10px 0 20px;">科室</label>
        <select id="ks" class="easyui-combobox" style="height: 25px; width: 160px;">
        </select>
        <label for="CYRQStart" style="padding:5px 10px 0 20px;">采样日期</label>
        <input type="text" class="form-control Wdate" style="width:100px;" name="sdate" id="sdate"
               placeholder=""
               value="${CYRQStart}"
               onclick="WdatePicker({ dateFmt: 'yyyy-MM-dd' })"/>
        <label for="CYRQEnd" style="padding:5px 10px 0 10px;">至</label>
        <input type="text" class="form-control Wdate" style="width:100px;" name="edate" id="edate"
               placeholder=""
               value="${CYRQEnd}"
               onclick="WdatePicker({ dateFmt: 'yyyy-MM-dd' })"/>

        <button type="button" class="btn btn-success bi-btn-search" onclick="BandMainGrid()">
            <span class="glyphicon glyphicon glyphicon-search"></span>
        </button>
        <div class="btn-group-right">
            <div class="btn-group" role="group">
                <button type="button" class="btn btn-default bi-btn-width90" id="tj" onclick="AddJyinfo()">添加</button>
                <button type="button" class="btn btn-default bi-btn-width90" id="xg" onclick="EditeJyinfo()">修改</button>
                <button type="button" class="btn btn-default bi-btn-width90" id="fs" onclick="UpdateJyinfo('已发送')">发送
                </button>
                <button type="button" class="btn btn-default bi-btn-width90" id="zf" onclick="UpdateJyinfo('已作废')">作废
                </button>
                <button type="button" class="btn btn-default bi-btn-width90" id="bcjyjg" onclick="SaveJyjg()">保存检验结果
                </button>
                <button type="button" class="btn btn-default bi-btn-width90" id="sh" onclick="UpdateJyinfo('已审核')">审核
                </button>
            </div>
        </div>
    </div>
</div>
<div style="width:100%;float:left;display:inline-block;padding-top:10px;">
    <table id="Maingrid" class="datagrid" style="width:100%"></table>
</div>
<div style="width:100%;float:left;display:inline-block;padding-top:10px;">
    <table id="Contentgrid" class="datagrid" style="width:100%"></table>
</div>
</body>
</html>
